﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using SalesModel;
using System.Data.SqlClient;

namespace SalesController
{
    public class SQLServerDAOCustomer : DAOCustomer
    {
        public void add(Customer c)
        {
            SqlConnection conn = null;
            try
            {
                //Paso 1 y 2: Registrar el driver y obtener la conexión
                conn = new SqlConnection();
                conn.ConnectionString = "Server=inti.lab.inf.pucp.edu.pe;" +
                    "Database=inf282;User ID=inf282;Password=inf282db;";
                conn.Open();

                //Paso 3. Preparar la sentencia
                SqlCommand comm = new SqlCommand();
                comm.Connection = conn;
                comm.CommandText = "INSERT INTO BoolCustomer(dni,address,email,phoneNumber,status,name," +
                "fLastName, mLastName, regDate) VALUES (@param1,@param2,@param3,@param4,@param5,@param6,@param7,@param8,@param9)";
                SqlParameter param1 = new SqlParameter("@param1",
                                                        System.Data.SqlDbType.VarChar);
                SqlParameter param2 = new SqlParameter("@param2",
                                                        System.Data.SqlDbType.VarChar);
                SqlParameter param3 = new SqlParameter("@param3",
                                                        System.Data.SqlDbType.VarChar);
                SqlParameter param4 = new SqlParameter("@param4",
                                                        System.Data.SqlDbType.VarChar);
                SqlParameter param5 = new SqlParameter("@param5",
                                                        System.Data.SqlDbType.VarChar);
                SqlParameter param6 = new SqlParameter("@param6",
                                                        System.Data.SqlDbType.VarChar);
                SqlParameter param7 = new SqlParameter("@param7",
                                                        System.Data.SqlDbType.VarChar);
                SqlParameter param8 = new SqlParameter("@param8",
                                                        System.Data.SqlDbType.VarChar);
                SqlParameter param9 = new SqlParameter("@param9",
                                                       System.Data.SqlDbType.Date);
                param1.Value = c.getDni();
                param2.Value = c.getAddress();
                param3.Value = c.getEmail();
                param4.Value = c.getPhoneNumber();
                param5.Value = c.getStatus();
                param6.Value = c.getName();
                param7.Value = c.getmLastName();
                param8.Value = c.getfLastName();
                param9.Value = c.getRegDate().ToShortDateString();

                comm.Parameters.Add(param1);
                comm.Parameters.Add(param2);
                comm.Parameters.Add(param3);
                comm.Parameters.Add(param4);
                comm.Parameters.Add(param5);
                comm.Parameters.Add(param6);
                comm.Parameters.Add(param7);
                comm.Parameters.Add(param8);
                comm.Parameters.Add(param9);

                //Paso 4. Ejecutar la sentencia
                comm.ExecuteNonQuery();
            }
            catch (Exception e)
            {
                // TODO Auto-generated catch block			
                Console.WriteLine(e.StackTrace); //Se debe manejar con logs

            }
            finally
            {
                //Paso 5. CERRAR LA CONEXIÓN!!!
                try { if (conn != null) conn.Close(); }
                catch (Exception e) { Console.WriteLine(e.StackTrace); }
            }
        }
        public void update(Customer c)
        {
            SqlConnection conn = null;
            try
            {
                //Paso 1 y 2: Registrar el driver y obtener la conexión
                conn = new SqlConnection();
                conn.ConnectionString = "Server=inti.lab.inf.pucp.edu.pe;" +
                    "Database=inf282;User ID=inf282;Password=inf282db;";
                conn.Open();

                //Paso 3. Preparar la sentencia
                SqlCommand comm = new SqlCommand();
                comm.Connection = conn;

                //comm.CommandText = "UPDATE BoolCustomer SET dni=@param1,email=@param2, name=@param3, " +
                //        "fLastName=@param4, mLastName=@param5 WHERE id=@param6";
                comm.CommandText = "UPDATE BoolCustomer SET dni=@param1, email=@param2, name=@param3, fLastName=@param4, mLastName=@param5,status=@param6 WHERE id=@param7";

                SqlParameter param1 = new SqlParameter("@param1",
                                                        System.Data.SqlDbType.VarChar);
                SqlParameter param2 = new SqlParameter("@param2",
                                                        System.Data.SqlDbType.VarChar);
                SqlParameter param3 = new SqlParameter("@param3",
                                                        System.Data.SqlDbType.VarChar);
                SqlParameter param4 = new SqlParameter("@param4",
                                                        System.Data.SqlDbType.VarChar);
                SqlParameter param5 = new SqlParameter("@param5",
                                                        System.Data.SqlDbType.VarChar);
                SqlParameter param6 = new SqlParameter("@param6",
                                                        System.Data.SqlDbType.Int);
                SqlParameter param7 = new SqlParameter("@param7",
                                                        System.Data.SqlDbType.Int);
                param1.Value = c.getDni();
                param2.Value = c.getEmail();
                param3.Value = c.getName();
                param4.Value = c.getfLastName();
                param5.Value = c.getmLastName();
                param6.Value = c.getId_customer();
                param7.Value = c.getStatus();

                comm.Parameters.Add(param1);
                comm.Parameters.Add(param2);
                comm.Parameters.Add(param3);
                comm.Parameters.Add(param4);
                comm.Parameters.Add(param5);
                comm.Parameters.Add(param6);
                comm.Parameters.Add(param7);

                //Paso 4. Ejecutar la sentencia
                comm.ExecuteNonQuery();
            }
            catch (Exception e)
            {
                // TODO Auto-generated catch block			
                Console.WriteLine(e.StackTrace); //Se debe manejar con logs
                throw e;
            }
            finally
            {
                //Paso 5. CERRAR LA CONEXIÓN!!!!!
                try { if (conn != null) conn.Close(); }
                catch (Exception e) { Console.WriteLine(e.StackTrace); }
            }
        }
        public void delete(String customerDni)
        {
            SqlConnection conn = null;
            try
            {
                //Paso 1 y 2: Registrar el driver y obtener la conexión
                conn = new SqlConnection();
                conn.ConnectionString = "Server=inti.lab.inf.pucp.edu.pe;" +
                    "Database=inf282;User ID=inf282;Password=inf282db;";
                conn.Open();

                //Paso 3. Preparar la sentencia
                SqlCommand comm = new SqlCommand();
                comm.Connection = conn;

                comm.CommandText = "DELETE FROM BoolCustomer " +
                                            " WHERE dni=@param1";
                SqlParameter param1 = new SqlParameter("@param1",
                                                        System.Data.SqlDbType.VarChar);
                param1.Value = customerDni;

                comm.Parameters.Add(param1);

                //Paso 4. Ejecutar la sentencia
                comm.ExecuteNonQuery();
            }
            catch (Exception e)
            {
                // TODO Auto-generated catch block			
                Console.WriteLine(e.StackTrace); //Se debe manejar con logs
                throw e;
            }
            finally
            {
                //Paso 5. CERRAR LA CONEXIÓN!!!!
                try { if (conn != null) conn.Close(); }
                catch (Exception e) { Console.WriteLine(e.StackTrace); }
            }
        }
        public List<Customer> queryAll()
        {
            List<Customer> list = new List<Customer>();
            SqlConnection conn = null;
            SqlDataReader myReader = null;
            try
            {
                //Paso 1 y 2: Registrar el driver y obtener la conexión
                conn = new SqlConnection();
                conn.ConnectionString = "Server=inti.lab.inf.pucp.edu.pe;" +
                    "Database=inf282;User ID=inf282;Password=inf282db;";
                conn.Open();

                //Paso 3. Preparar la sentencia
                SqlCommand comm = new SqlCommand();
                comm.Connection = conn;
                comm.CommandText = "SELECT * FROM BoolCustomer";

                //Paso 4. Ejecutar la sentencia
                myReader = comm.ExecuteReader();

                //Paso 5: Evaluar los resultados
                while (myReader.Read())
                {
                    Customer c = new Customer();
                    c.setId_customer(Int32.Parse(myReader["id"].ToString()));
                    c.setDni(myReader["dni"].ToString());
                    c.setAddress(myReader["Address"].ToString());
                    c.setEmail(myReader["email"].ToString());
                    c.setPhoneNumber(myReader["phoneNumber"].ToString());
                    c.setStatus(Int32.Parse(myReader["status"].ToString()));
                    //c.setRegDate(DateTime.Parse(myReader["regDate"].ToString()));
                    c.setName(myReader["name"].ToString());
                    c.setfLastName(myReader["fLastName"].ToString());
                    c.setmLastName(myReader["mLastName"].ToString());
                    list.Add(c);
                }
            }
            catch (Exception e)
            {
                // TODO Auto-generated catch block			
                Console.WriteLine(e.StackTrace); //Se debe manejar con logs
                throw e;
            }
            finally
            {
                //Paso 6. CERRAR LA CONEXIÓN!!!!
                try { if (myReader != null) myReader.Close(); }
                catch (Exception e) { Console.WriteLine(e.StackTrace); }
                try { if (conn != null) conn.Close(); }
                catch (Exception e) { Console.WriteLine(e.StackTrace); }
            }
            return list;
        }
        public Customer queryById(int customerId)
        {
            Customer c = null;
            SqlConnection conn = null;
            SqlDataReader myReader = null;
            try
            {
                //Paso 1 y 2: Registrar el driver y obtener la conexión
                conn = new SqlConnection();
                conn.ConnectionString = "Server=inti.lab.inf.pucp.edu.pe;" +
                    "Database=inf282;User ID=inf282;Password=inf282db;";
                conn.Open();

                //Paso 3. Preparar la sentencia
                SqlCommand comm = new SqlCommand();
                comm.Connection = conn;
                comm.CommandText = "SELECT * FROM BoolCustomer WHERE id=@param1";
                SqlParameter param1 = new SqlParameter("@param1",
                                                        System.Data.SqlDbType.Int);
                param1.Value = customerId;
                comm.Parameters.Add(param1);

                //Paso 4. Ejecutar la sentencia
                myReader = comm.ExecuteReader();

                //Paso 5: Evaluar los resultados
                if (myReader.Read())
                {
                    c = new Customer();
                    c.setId_customer(Int32.Parse(myReader["id"].ToString()));
                    c.setDni(myReader["dni"].ToString());
                    c.setAddress(myReader["address"].ToString());
                    c.setEmail(myReader["email"].ToString());
                    c.setPhoneNumber(myReader["phoneNumber"].ToString());
                    c.setStatus(Int32.Parse(myReader["status"].ToString()));
                    //c.setRegDate(DateTime.Parse(myReader["regDate"].ToString()));
                    c.setName(myReader["name"].ToString());
                    c.setfLastName(myReader["fLastName"].ToString());
                    c.setmLastName(myReader["mLastName"].ToString());
                }
            }
            catch (Exception e)
            {
                // TODO Auto-generated catch block			
                Console.WriteLine(e.StackTrace); //Se debe manejar con logs
                throw e;
            }
            finally
            {
                //Paso 5. CERRAR LA CONEXIÓN!!!!
                try { if (myReader != null) myReader.Close(); }
                catch (Exception e) { Console.WriteLine(e.StackTrace); }
                try { if (conn != null) conn.Close(); }
                catch (Exception e) { Console.WriteLine(e.StackTrace); }
            }
            return c;
        }
        public int count()
        {
            int quantity = 0;
            SqlConnection conn = null;
            SqlDataReader myReader = null;
            try
            {
                //Paso 1 y 2: Registrar el driver y obtener la conexión
                conn = new SqlConnection();
                conn.ConnectionString = "Server=inti.lab.inf.pucp.edu.pe;" +
                    "Database=inf282;User ID=inf282;Password=inf282db;";
                conn.Open();

                //Paso 3. Preparar la sentencia
                SqlCommand comm = new SqlCommand();
                comm.Connection = conn;
                comm.CommandText = "SELECT Count(1) AS quantity FROM BoolCustomer";
                //Paso 4. Ejecutar la sentencia
                myReader = comm.ExecuteReader();

                //Paso 5: Evaluar los resultados
                if (myReader.Read())
                {
                    quantity = Int32.Parse(myReader["quantity"].ToString());
                }
            }
            catch (Exception e)
            {
                // TODO Auto-generated catch block			
                Console.WriteLine(e.StackTrace); //Se debe manejar con logs
                throw e;
            }
            finally
            {
                //Paso 5. CERRAR LA CONEXIÓN!!!
                try { if (myReader != null) myReader.Close(); }
                catch (Exception e) { Console.WriteLine(e.StackTrace); }
                try { if (conn != null) conn.Close(); }
                catch (Exception e) { Console.WriteLine(e.StackTrace); }
            }
            return quantity;
        }
    }
}
